﻿namespace Vdc.Grid.Design
{
    using Vdc.Grid;
    using System;
    using System.ComponentModel;
    using System.Drawing.Design;
    using System.Windows.Forms.Design;

    internal class GridTemplateSettingsEditor : UITypeEditor
    {
        private IWindowsFormsEditorService editorService;

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (provider != null)
            {
                this.editorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
            }
            if (this.editorService != null)
            {
                Vdc.Grid.Grid instance = (Vdc.Grid.Grid) context.Instance;
                GridTemplateSettingsSetupControl dialog = new GridTemplateSettingsSetupControl((GridTemplateSettings) value, this.editorService, instance.GetTemplatesIds());
                this.editorService.ShowDialog(dialog);
                if (dialog.TemplateSettings != null)
                {
                    return new GridTemplateSettings { FilterApplyButton_TemplateId = dialog.TemplateSettings.FilterApplyButton_TemplateId, FilterHideButton_TemplateId = dialog.TemplateSettings.FilterHideButton_TemplateId, FilterRemoveButton_TemplateId = dialog.TemplateSettings.FilterRemoveButton_TemplateId, FilterShowButton_TemplateId = dialog.TemplateSettings.FilterShowButton_TemplateId, GroupHeaderTemplateId = dialog.TemplateSettings.GroupHeaderTemplateId, HeadingTemplateId = dialog.TemplateSettings.HeadingTemplateId, ManualPagingTemplateId = dialog.TemplateSettings.ManualPagingTemplateId, MultiRecordSaveCancel_TemplateId = dialog.TemplateSettings.MultiRecordSaveCancel_TemplateId, NewRecord_EditTemplateId = dialog.TemplateSettings.NewRecord_EditTemplateId, NewRecord_TemplateId = dialog.TemplateSettings.NewRecord_TemplateId, RowEditTemplateId = dialog.TemplateSettings.RowEditTemplateId };
                }
            }
            return value;
        }

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }
    }
}

